#ifndef _THREAD_
#define _THREAD_

#include <pthread.h>
#include <string>
#include <iostream>
#include "log.hpp"

typedef void* (*func_t) (void*);  // 定义函数指针

class ThreadData
{
public:
    std::string _name;
    void* _args;
};

class Thread
{
public:
    Thread(int num, func_t func, void* args)
    :_func(func)
    {
        _data._args = args;
        // _data._name = "thread" + std::to_string(num);
        char buffer[64];
        snprintf(buffer, sizeof buffer, "%s-%d", "thread", num);
        _data._name = buffer;
    }

    void start()
    {
        // 启动线程
        pthread_create(&_tid, nullptr, _func, (void*)&_data);
        // std::cout << _data._name << "启动成功！" << std::endl;
        logMessage(WARNING, "%s:%s", _data._name.c_str(), "启动成功");
    }

    void join()
    {
        pthread_join(_tid, nullptr);
    }

    ~Thread()
    {}
private:
    pthread_t _tid;
    func_t _func;
    ThreadData _data;
};

#endif